/*
Name:System_status
Author: Terry Dong
Description: data access layer
*/
using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Reflection;
using Gammar.Digger.SystemFramework;
using Gammar.Digger.Entity;
using Gammar.Digger.IDAL;
using Gammar.Digger.Data;
namespace Gammar.Digger.DALSqlServer
{
	public partial class System_status:ISystem_status
	{
		private DbSqlServer helper;
		
		#region constructors
		/// <summary>
        /// default constructor with the defaultConn
        /// </summary>
        public System_status()
        {
			helper = new DbSqlServer(ConnectionString.SqlServer_GPriceCollect);
        }
		
		/// <summary>
        /// default constructor with the defaultConn
        /// </summary>
		public System_status(string connString)
		{
			helper = new DbSqlServer(connString);	
		}
		#endregion
		
		#region IDataBase<System_status> 

        public int Add(System_statusEntity entity)
        {
           	StringBuilder strSql=new StringBuilder();
			strSql.Append("INSERT INTO System_status(");
			strSql.Append("status,start_time,end_time,sum_plugins,sum_web_site)");
			strSql.Append(" VALUES (");
			strSql.Append("@status,@start_time,@end_time,@sum_plugins,@sum_web_site)");
			SqlParameter[] parameters = {
					new SqlParameter("@status",SqlDbType.Int),
					new SqlParameter("@start_time",SqlDbType.BigInt),
					new SqlParameter("@end_time",SqlDbType.BigInt),
					new SqlParameter("@sum_plugins",SqlDbType.Int),
					new SqlParameter("@sum_web_site",SqlDbType.Int)
					};
				parameters[0].Value = entity.status;
				parameters[1].Value = entity.start_time;
				parameters[2].Value = entity.end_time;
				parameters[3].Value = entity.sum_plugins;
				parameters[4].Value = entity.sum_web_site;

                strSql.Append("select SCOPE_IDENTITY() as id;");
			object obj=  helper.ExecuteCell(strSql.ToString(), parameters);
            return obj == null ? -1 : Convert.ToInt32(obj);
        }
		
        public int Update(System_statusEntity entity)
        {
			
           StringBuilder strSql=new StringBuilder();
			strSql.Append("UPDATE System_status SET ");
				strSql.Append("status=@status,");
				strSql.Append("start_time=@start_time,");
				strSql.Append("end_time=@end_time,");
				strSql.Append("sum_plugins=@sum_plugins,");
				strSql.Append("sum_web_site=@sum_web_site");
		
			strSql.Append(" WHERE sys_id=@sys_id");
			SqlParameter[] parameters = {
					new SqlParameter("@sys_id",SqlDbType.Int),
					new SqlParameter("@status",SqlDbType.Int),
					new SqlParameter("@start_time",SqlDbType.BigInt),
					new SqlParameter("@end_time",SqlDbType.BigInt),
					new SqlParameter("@sum_plugins",SqlDbType.Int),
					new SqlParameter("@sum_web_site",SqlDbType.Int)
					};
				parameters[0].Value = entity.sys_id;
				parameters[1].Value = entity.status;
				parameters[2].Value = entity.start_time;
				parameters[3].Value = entity.end_time;
				parameters[4].Value = entity.sum_plugins;
				parameters[5].Value = entity.sum_web_site;
				
			return helper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
		
		public int UpdateBysys_id(System_statusEntity entity)
        { 
            string where=string.Format("sys_id='{0}'",entity.sys_id);
			entity.sys_id=null;
        	return Updatepartial(entity, where);
		}

        public int Delete(string strWhere)
        {
           StringBuilder strSql=new StringBuilder();
			strSql.Append("DELETE FROM System_status ");
			if(!string.IsNullOrEmpty(strWhere))
			{
				strSql.Append(" WHERE "+strWhere);
			}
			return helper.ExecuteNonQuery(strSql.ToString());
        }

        public System_statusEntity GetEntity(int primaryKeyId)
        {
            StringBuilder strSql=new StringBuilder();
			strSql.Append("select * from System_status ");
			strSql.Append(" where sys_id=@primaryKeyId");
			SqlParameter[] parameters = {
					new SqlParameter("@primaryKeyId", SqlDbType.Int)};
			parameters[0].Value = primaryKeyId;
			SqlDataReader dr = helper.ExecuteReader(strSql.ToString(), parameters) as SqlDataReader;
            if (dr.HasRows)
            {
                dr.Read();
				System_statusEntity entity=new System_statusEntity();
				if(!Convert.IsDBNull(dr["sys_id"]))
                {
                    entity.sys_id = (int)dr["sys_id"];
                }
				if(!Convert.IsDBNull(dr["status"]))
                {
                    entity.status = (int)dr["status"];
                }
				if(!Convert.IsDBNull(dr["start_time"]))
                {
                    entity.start_time = (long)dr["start_time"];
                }
				if(!Convert.IsDBNull(dr["end_time"]))
                {
                    entity.end_time = (long)dr["end_time"];
                }
				if(!Convert.IsDBNull(dr["sum_plugins"]))
                {
                    entity.sum_plugins = (int)dr["sum_plugins"];
                }
				if(!Convert.IsDBNull(dr["sum_web_site"]))
                {
                    entity.sum_web_site = (int)dr["sum_web_site"];
                }
				dr.Close();
            	dr.Dispose();
				return entity;
			}
			else
			{
				dr.Close();
            	dr.Dispose();
				return null;
			}
        }

        public object GetCell(string cellName, string where)
        {
            string strSql = string.Empty;
            if (string.IsNullOrEmpty(where))
            {
                strSql = string.Format("SELECT {0} FROM System_status", cellName);
            }
            else
            {
                strSql = string.Format("SELECT {0} FROM System_status Where {1}", cellName, where);
            }
            return helper.ExecuteCell(strSql);
        }

		public DataTable GetTable(string strWhere)
		{
           StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT sys_id,status,start_time,end_time,sum_plugins,sum_web_site");
			strSql.Append(" FROM System_status ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" WHERE "+strWhere);
			}
			return helper.ExecuteAdapter(strSql.ToString());
		}
		
        public SortList<System_statusEntity> GetList(string strWhere)
        {
            StringBuilder strSql=new StringBuilder();
			strSql.Append("select sys_id,status,start_time,end_time,sum_plugins,sum_web_site");
			strSql.Append(" FROM System_status ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			SqlDataReader dr = helper.ExecuteReader(strSql.ToString()) as SqlDataReader;
			SortList<System_statusEntity> list= new SortList<System_statusEntity>();
			while (dr.Read())
            {
				System_statusEntity entity=new System_statusEntity();
				if(!Convert.IsDBNull(dr["sys_id"]))
                {
                    entity.sys_id = (int)dr["sys_id"];
                }
				if(!Convert.IsDBNull(dr["status"]))
                {
                    entity.status = (int)dr["status"];
                }
				if(!Convert.IsDBNull(dr["start_time"]))
                {
                    entity.start_time = (long)dr["start_time"];
                }
				if(!Convert.IsDBNull(dr["end_time"]))
                {
                    entity.end_time = (long)dr["end_time"];
                }
				if(!Convert.IsDBNull(dr["sum_plugins"]))
                {
                    entity.sum_plugins = (int)dr["sum_plugins"];
                }
				if(!Convert.IsDBNull(dr["sum_web_site"]))
                {
                    entity.sum_web_site = (int)dr["sum_web_site"];
                }
                list.Add(entity);
            }

            dr.Close();
            dr.Dispose();

            return list;
        }
		
				
		#region select,add,update the fields that you want to operate.
		
		public int AddPartial(System_statusEntity entity)
        {
            object reval=null;
            string sql = string.Empty;
            SqlParameter[] param = DataUtility.BuildAdd(entity, "System_status", ref sql);

            if (param!=null)
            {
                reval = helper.ExecuteCell(sql + "select SCOPE_IDENTITY() as id;", param);
            }
           return reval==null?-1:Convert.ToInt32(reval);            
            
        }
		
		public int Updatepartial(System_statusEntity entity, string where)
		{
            int reval = 0;
            string sql = string.Empty;
            SqlParameter[] param = DataUtility.BuildUpdate(entity, "System_status",where, ref sql);

            if (param != null)
            {
                reval = helper.ExecuteNonQuery(sql,param);
            }
            return reval;     
		}
		
		public List<System_statusEntity> GetPartial(string[] selectedFields,string where)
        {
            List<System_statusEntity> list = new List<System_statusEntity>();
            string sql = DataUtility.BuildSelect(selectedFields, "System_status", where);
            if (!string.IsNullOrEmpty(sql))
            {
                DbDataReader reader= helper.ExecuteReader(sql);
             
                while (reader.Read())
                {
                    System_statusEntity model = new System_statusEntity();
                    foreach (string field in selectedFields)
                    {
                        PropertyInfo propInfo = model.GetType().GetProperty(field);
                        propInfo.SetValue(model, reader[field], null);
                    }

                    list.Add(model);
                }

                reader.Close();
            }
            return list;
        }
		#endregion
		

        #endregion
	}
}
